package com.example.xiaomaotv;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.content.SharedPreferences;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity {
    private EditText accountEdit;
    private EditText passwordEdit;
    private TextView modify;
    private CheckBox saveCheckBox;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private MySQL helper;
    private SQLiteDatabase database;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_login);
        //初始化数据
        initViews();
        initDatabase();
        initSharedPreferences();
        //记住密码部分
        rememberPassword();
        //修改密码部分
        modifyPassword();
    }
    private void initViews() {
        accountEdit = findViewById(R.id.et_account);
        passwordEdit = findViewById(R.id.et_password);
        saveCheckBox = findViewById(R.id.checkbox_btn);
        modify = findViewById(R.id.text_modify);
    }
    private void initDatabase() {
        helper = new MySQL(LoginActivity.this, "XiaoMaoTv", null, 1);
        database = helper.getWritableDatabase();
    }
    private void initSharedPreferences() {
        sp = getSharedPreferences("userInfo", MODE_PRIVATE);
        editor = sp.edit();
    }
    //记住密码
    private void rememberPassword() {
        Boolean saveState = sp.getBoolean("saveState", false);
        String savedAccount = sp.getString("account", "");
        accountEdit.setText(savedAccount);
        if (saveState) {
            String savedPassword = sp.getString("password", "");
            passwordEdit.setText(savedPassword);
            saveCheckBox.setChecked(true);
        } else {
            passwordEdit.setText("");
            saveCheckBox.setChecked(false);
        }
    }
    //修改密码
    private void modifyPassword() {
        modify.setOnClickListener(v -> {
            String savedAccount = accountEdit.getText().toString().trim();
            if (TextUtils.isEmpty(savedAccount)) {
                showToast("请先输入账号");
                return;
            }
            // 检查账号是否存在
            Cursor cursor = database.query("user", null, "account=?", new String[]{savedAccount}, null, null, null);
            if (cursor.getCount() == 0) {
                showToast("该账号不存在");
                cursor.close();
                return;
            }
            cursor.close();
            // 保存当前输入的账号
            editor.putString("account", savedAccount);
            editor.apply();
            Intent intent = new Intent(LoginActivity.this, ModifyActivity.class);
            startActivity(intent);
        });
    }
    //点击登录
    public void BT_logIn_onClick(View view) {
        String accountInput = accountEdit.getText().toString();
        String passwordInput = passwordEdit.getText().toString();
        Boolean isChecked = saveCheckBox.isChecked();
        //搜索数据库中与输入账号相同的数据
        Cursor cursor = database.query("user",null,"account=?",new String[]{accountInput},null,null,null);
        if (accountInput.isEmpty() || passwordInput.isEmpty()) {
            showToast("账号或密码不能为空(!_!)");
        } else if(cursor.getCount()==0) {
            showToast("账号未注册，请先注册！！！");
        } else {
            cursor.moveToNext();
            String password = cursor.getString(cursor.getColumnIndex("password"));
            if (password.equals(passwordInput)){
                editor.putString("account",accountInput);
                editor.putString("password", passwordInput);
                editor.apply();
                editor.putBoolean("saveState", isChecked);
                editor.apply();
                showToast("登录成功\n      >_<");
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
            }else {
                showToast("登录失败\n   @_@\n密码错误");
            }
        }
    }
    //点击注册
    public void BT_signUp_onClick(View view) {
        Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
        startActivity(intent);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (database != null && database.isOpen()) {
            database.close();
        }
    }
    private void showToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}